﻿using System;
using System.Diagnostics;

using Autofac;

using MiP.Common;
using MiP.XmlCmd.IoC;
using MiP.XmlCmd.Shell;

namespace MiP.XmlCmd
{
    internal class Program
    {
        private static readonly ILogger _logger = RootContainer.Instance.Resolve<ILogger>();

        private static int Main(string[] args)
        {
            try
            {
                IProgramRunner runner = RootContainer.Instance.Resolve<IProgramRunner>();
                runner.Run(args);
                return 0;
            }
            catch (Exception ex)
            {
                // program crashes way harder, when ILogger can not be resolved, even before Main() is called.

                _logger.Error(ex.Message);
#if DEBUG
                Console.WriteLine(ex.ToString()); // I do not care about log level when debugging.
#endif
                return 4;
            }
            finally
            {
                if (IsVisualStudioHostProcess)
                {
                    Console.WriteLine("Press any key..."); // I also do not care about loglevel when the process was called from visual studio, even if its a release build.
                    Console.ReadKey();
                }
            }
        }

        protected static bool IsVisualStudioHostProcess
        {
            get
            {
                return Process.GetCurrentProcess().ProcessName.Contains(".vshost");
            }
        }
    }
}